package com.ts.proj.json;

import com.ts.base.controller.BaseController;
import com.ts.common.constant.AuthConstant;
import com.ts.common.constant.ConsoleConstant;
import com.ts.common.constant.SystemUserTableColumnConstant;
import com.ts.proj.entity.SystemRoleEntity;
import com.ts.proj.entity.SystemUserEntity;
import com.ts.proj.service.SystemRoleService;
import com.ts.proj.service.SystemUserService;
import com.ts.util.JsonUtils;
import com.ts.util.PageData;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class IndexJson extends BaseController {

    @Autowired
    private SystemUserService systemUserService;

    @Autowired
    private SystemRoleService systemRoleService;

    /**
     * 重登录
     * @return
     */
    @RequestMapping(value = "/login_tologin/{loginName}/{loginPass}",method = RequestMethod.POST)
    public Map<String,String> login(@PathVariable("loginName") String loginName,@PathVariable("loginPass") String loginPass){
        Map<String, String> resultMap = new HashMap<>();
        PageData pd = new PageData();
        pd.put(SystemUserTableColumnConstant.USERNAME, loginName);
        List<SystemUserEntity> systemUserEntityList = systemUserService.findAll(pd);
        if(null == systemUserEntityList ||0== systemUserEntityList.size()){
            resultMap.put(ConsoleConstant.MSG, AuthConstant.USERNOTEXIST);
            return resultMap;
        }else{
            SystemUserEntity systemUserEntity = systemUserEntityList.get(0);
            String secret = new Md5Hash(loginPass, systemUserEntity.getUsername() + systemUserEntity.getSalt()).toString();
            if(!secret.equals(systemUserEntity.getPassword())){
                resultMap.put(ConsoleConstant.MSG, AuthConstant.USERNAMEORPASSISERROR);
                return resultMap;
            }
            if(!systemUserEntity.getLocked()){
                resultMap.put(ConsoleConstant.MSG, AuthConstant.USERISLOCKED);
                return resultMap;
            }
            String rights = "";
            String btnRights = "";
            try{
                PageData pageData = new PageData();
                pageData.put("id", systemUserEntity.getRoleId());
                List<SystemRoleEntity> allOrOne = systemRoleService.findAll(pageData);
                rights = allOrOne.get(0).getRights();
                btnRights = allOrOne.get(0).getBtnRights();
            }catch (Exception e){

            }


            //登陆成功，存入session
            HttpSession session = this.getRequest().getSession();
            //将用户存入session
            session.setAttribute(ConsoleConstant.SESSIONUSER, JsonUtils.objectToJson(systemUserEntity));
            //将页面权限传入session
            session.setAttribute(ConsoleConstant.RIGHTS, rights);
            session.setAttribute(ConsoleConstant.ROLEID, systemUserEntity.getRoleId());
            session.setAttribute(ConsoleConstant.BUTTONRIGHTS, btnRights);
            resultMap.put(ConsoleConstant.MSG, AuthConstant.SUCCESS);
            return resultMap;
        }
    }
}
